﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;

namespace shfa.snomtastic.admin
{
    public partial class _default : System.Web.UI.Page
    {
        #region Private Members
        const string eControlPath = "~/editcontrols/";
        const string eIconPath = "~/images/icons/";

        private EditBase ActiveControl { get; set; }

        private string CurrentControl
        {
            get
            {
                return (ViewState["CurrentControl"] != null) ? ViewState["CurrentControl"].ToString() : string.Empty;
            }
            set
            {
                ViewState["CurrentControl"] = value;
            }
        }


        private string getControlId(string controlName)
        {
            controlName = controlName.Split('.')[0];
            return controlName.Replace("/", "").Replace("~", "");
        }

        #endregion

        #region Page


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BuildTree();
            }

            if (CurrentControl != String.Empty)
            {
                LoadEditControl(CurrentControl);
            }
            else
            {
                LoadEditControl("ePhones.ascx");
            }

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            ibRebindTree.Click += new ImageClickEventHandler(ibRebindTree_Click);
            lbAppConfig.Click += new EventHandler(lbAppConfig_Click);
            rtNavigation.NodeClick += new RadTreeViewEventHandler(rtNavigation_NodeClick);
            rtNavigation.NodeDataBound += new RadTreeViewEventHandler(rtNavigation_NodeDataBound);
            abMain.ActionEvent += new shfa.snomtastic.admin.controls.ActionButtons.ActionEventHandler(abMain_ActionEvent);
        }


        #endregion

        #region Event Handlers
        protected void ibRebindTree_Click(object sender, ImageClickEventArgs e)
        {
            BuildTree();
            upNavigation.Update();

        }

        protected void lbAppConfig_Click(object sender, EventArgs e)
        {
            LoadEditControl("eAppConfig.ascx", new Guid());
            upActions.Update();
            upBody.Update();
        }



        protected void abMain_ActionEvent(object sender, ActionClickArgs e)
        {
            ActiveControl.EditBase_ActionEvent(this, e);
        }

        protected void ctrl_NavEvent(object sender, NavEventArgs e)
        {
            LoadEditControl(e.ControlName + ".ascx", e.ItemId, e.ParentId);


        }

        protected void rtNavigation_NodeClick(object sender, RadTreeNodeEventArgs e)
        {

            SiteNavController snc = new SiteNavController();
            Guid id = new Guid(e.Node.Value);
            List<SiteNavInfo> sniList = snc.GetSiteNavigationElements();
            string controlUrl = string.Empty;
            foreach (SiteNavInfo sni in sniList)
            {
                if (sni.SiteNavId == id)
                    controlUrl = sni.ControlUrl;
            }
            if (controlUrl != string.Empty)
            {
                LoadEditControl(controlUrl, id);
            }

            snc = null;

            upActions.Update();
            upBody.Update();
        }

        #endregion

        #region Navigation

        protected void rtNavigation_NodeDataBound(object sender, RadTreeNodeEventArgs e)
        {
            if (((SiteNavInfo)(e.Node.DataItem)).ImageUrl != string.Empty)
                e.Node.ImageUrl = eIconPath + ((SiteNavInfo)(e.Node.DataItem)).ImageUrl;

        }




        private void BuildTree()
        {
            SiteNavController snc = new SiteNavController();
            List<SiteNavInfo> sniList = snc.GetSiteNavigationElements();


            rtNavigation.DataSource = sniList;
            rtNavigation.DataTextField = "Name";
            rtNavigation.DataValueField = "SiteNavId";
            rtNavigation.DataFieldID = "SiteNavId";
            rtNavigation.DataFieldParentID = "ParentId";

            rtNavigation.DataBind();

            snc = null;

        }

        #endregion

        #region Helpers
        private void LoadEditControl(string controlName)
        {
            LoadEditControl(controlName, new Guid(), new Guid());
        }

        private void LoadEditControl(string controlName, Guid ItemId)
        {
            LoadEditControl(controlName, ItemId, new Guid());
        }

        private void LoadEditControl(string controlName, Guid ItemId, Guid ParentId)
        {


            if (CurrentControl != null)
            {
                Control previousControl = pnlBody.FindControl(getControlId(CurrentControl));
                if (!Object.Equals(previousControl, null))
                {
                    this.pnlBody.Controls.Remove(previousControl);
                }
            }
            string userControlID = getControlId(controlName);
            Control targetControl = pnlBody.FindControl(userControlID);
            if (Object.Equals(targetControl, null))
            {

                UserControl userControl = (UserControl)this.LoadControl(eControlPath + controlName);
                CurrentControl = controlName;
                //slashes and tildes are forbidden
                userControl.ID = userControlID;
                ((EditBase)userControl).ItemId = ItemId;
                ((EditBase)userControl).ParentId = ParentId;

                this.pnlBody.Controls.Add(userControl);

                ActiveControl = (EditBase)this.pnlBody.FindControl(userControlID);
                ActiveControl.NavEvent += new EditBase.NavEventHandler(ctrl_NavEvent);
                abMain.UpdateButtons(ActiveControl.ActionList);

            }


            upActions.Update();
            upBody.Update();

        }


        #endregion


    }
}
